Say if a function accessed this element in an array called c. What does it mean?
Say if a function accessed this element in an array called c. What does it mean?
It is technically undefined, if c is an array. If c is a pointer INTO an array, then I think it is still undefined by the standard, but it's possible that it "works" correctly - getting the element BEFORE c.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
c was a pointer so it did mean one element before. Thanks.
Citing from my copy of the standard:
I read this as "if c[-1] is an object of the same array as the one that contains c[0], then it's ok". Besides, the subscript operator in E1[E2] is defined as (*((E1) + (E2))) where E1 is a pointer and E2 is an integer.J.2 Undefined behavior
The behavior is undefined in the following circumstances:
[...]
- Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that does not point into, or just beyond, the same array object (6.5.6).
Hence, the following should be ok:
Greets,Code:int a[8]; int *c = a + 4; if(c[-1] == a[3]) puts("works");
Philip
Last edited by Snafuist; 04-22-2009 at 08:24 AM.
All things begin as source code.
Source code begins with an empty file.
-- Tao Te Chip
In the case you site there then c[-1] is technically a valid statement. But if you have
then c[-1] would return an unknown value since that address isn't part of the array c. I think this is more in the nature of the original question.Code:int c[8];
So while c[-1] isn't undefined, except in the case you refer to, it is an unknown value.
I agree with snafuist. It should be fine and equal to *(a - 1)...
I don't think that is considered undefined behavior. c[-1] returns the "value" stored in the address that is one before c[0].
Although, I guess that if c[0] is stored on address "0" or the lowest possible address, then c[-1] would generate an error. Or of c[0] is one address after the end of system protected memory.
I append my original statement. c[-1] yields undefined behavior.
why will C compiler give error if you try to access a memory.....
the only differnece here is you haven't stored any value there...
it will definitely print some value...but it will be a GARBAGE.
Python....whereas prints the value of last element of array
...suppose i have array of 5 elements then a[-1] is equivalent to a[4]
...but thats in PYTHON
But if store some value there...it will print it
exampleCode:#include<stdio.h> int main() { int c[] = {2,5,6,8,3,6}; c[-1] = 57; printf("%d",c[-1]); return 0; }Code:OUTPUT: 57
Yes you can do that, but you are likely to get a seg fault or other unexpected behavior if you do something like that.
Just because something works SOMETIMES doesn't mean it will work ALL times.
The memory you're writing to isn't allocated, so it's undefined.
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
Compiler doesn't play dice.....
Can u make out when does it give segmentation fault and when the garbage value...
You could be overwriting something important that is located in front of the array in memory?
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.